package hello;

import hello.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import hello.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
public class GreetingController {
    private String names;
    private String ids;

    @Autowired
    private UserService userService;

    @RequestMapping("/greeting")
    public Object greeting(HttpServletResponse response,
                           Model model) {
        response.setHeader("Access-Control-Allow-Origin", "*");

        return this.userService.findAll();
    }

    @RequestMapping("/greeting2")
    public String greeting2(@RequestParam String name,
                            String id,
                            HttpServletResponse response,
                            HttpServletRequest request
    ) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        set(id, name);
        return id + name;
    }

    public void set(String id, String name) {
//        this.names = name;
//        this.ids = id;
       this.userService.save(new User(id,name));
    }


}